<template>
	<view class="container">
		<div class="userlist mgb300">
			<div class="item" v-for="(item,index) in students" :key="index"  @click="chooseImage(item)">
				<div class="imgBg" v-if="item.photo">
					<image :src="item.photo" mode="widthFix"></image>
				</div>
				<div class="addItem" v-else>
					<u-icon name="plus-circle" color="#cccccc" size="100"></u-icon>
				</div>
				<h3>{{ item.name }}</h3>
			</div>
		</div>
	</view>
</template>

<script>
	const db = wx.cloud.database()
	export default {
		name: 'headcomd',
		data() {
			return {
				showPrivacy: false,
				xbls:"",
				students:[]
			};
		},
		onShareAppMessage(options) {},
		onLoad(options) {
			this.xbls = options.xbls
			this.getStudents(0)
			if (wx.getPrivacySetting) {
				wx.getPrivacySetting({
					success: res => {
						if (res.needAuthorization) {
							this.showPrivacy = true
						} else {
							
						}
					},
					fail: () => {},
					complete: () => {}
				})
			}
		},
		computed: {
			
		},
		mounted() {
			let that = this
			
		},
		created() {
			let that = this;
			
		},
		methods: {
			handleAgreePrivacyAuthorization() {
				this.showPrivacy = false
			},
			handleOpenPrivacyContract() {
				// 打开隐私协议页面
				wx.openPrivacyContract({
					success: () => {}, // 打开成功
					fail: () => {}, // 打开失败
					complete: () => {}
				})
			},
			getStudents(page){
				let that = this
				db.collection('24gk_students').where({
					xbls: that.xbls
				}).skip(page * 20).limit(20).get({
					success: res=>{
						if(res.data.length > 0){
							that.students = that.students.concat(res.data)
							let p = page + 1
							that.getStudents(p)
						}
					}
				})
			},
			addImg(){
				let that = this
				 
			},
			chooseImage(info) {
				var that = this;
				wx.chooseImage({
					count: 1, // 默认9
					sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
					success: function(res) {
						// 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
						var tempFilePaths = res.tempFilePaths
						that.imgs = tempFilePaths
						that.upLoadImage(info)
						
					},
					fail:function(req){
						console.log(req)
					}
				})
			},
			upLoadImage(info) {
				let that = this
				wx.showLoading({
					title: '图片上传中',
				})
				wx.uploadFile({
					url: 'https://www.baixuemianshi.com/zhuanti/upload_img/xcx2.php', //仅为测试地址，填写你的url即可
					filePath: that.imgs[0],
					name: 'file',
					success: function(res) {
						if (res.statusCode == 200) {
							let path = JSON.parse(res.data)
							that.changeUserPhoto(info,path)
						}
					},
					error: function(res) {
						console.log("上传失败!")
					},
				})
				wx.hideLoading();
			},
			changeUserPhoto(info,path){
				let that = this
				db.collection('24gk_students').doc(info._id).update({
					data:{
						photo: 'https://www.baixuemianshi.com/zhuanti/upload_img/upload/' + path.data
					},
					success: res=>{
						that.students = []
						that.getStudents(0)
					}
				})
			}
		}
	};
</script>

<style>
	@import url("./index.css");
</style>